package data

import (
	"fmt"

	"mig/pkg/chunk"
	"mig/pkg/types"
)

type Database interface {
	Source
	Target

	Count() (int64, error)
	Identifier() string
	Execute(sql string) error
}

type Source interface {
	ReadChunk(chunkNo, chunkSize int64) (chunk.Chunk, error)
}

type Target interface {
	WriteChunk(chunk chunk.Chunk) error
}

func NewDatabase(cfg *types.Config, isSource bool) Database {
	dbCfg := cfg.GetDatabaseConfig(isSource)
	schema := cfg.GetSchema(isSource)
	switch {
	case dbCfg.Mysql != nil:
		return NewMysql(dbCfg.Mysql, schema, cfg.Algorithm)
	case dbCfg.Mongo != nil:
		return NewMongoDB(dbCfg.Mongo, schema, cfg.Algorithm)
	default:
		fmt.Println("unsupported database")
	}
	return nil
}
